package cate.game.util.api;

import cate.game.GameBody;
import cate.game.role.Role;
import cp.solution.business.game.role.NameRegEntity;
import cp.solution.business.gm.BatchMailEntity;
import cp.solution.business.gm.dto.BatchMailListReq;
import cp.solution.business.gm.dto.BatchMailListResp;
import easy.java.practice.errorcode.EcException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ConsoleApi {

    public static void regName(GameBody game, Role role, byte regType) throws EcException {
        NameRegEntity nr = buildNameReg(game, role, regType);
        nr.setRoleName(role.getBase().name);
        game.apis().console.nameReg.reg(nr);
    }

    public static void regNameAndSet(GameBody game, Role role, String newName, byte regType) throws EcException {
        NameRegEntity nr = buildNameReg(game, role, regType);
        nr.setRoleName(newName);
        game.apis().console.nameReg.reg(nr);
        role.getBase().name = newName;
    }

    private static NameRegEntity buildNameReg(GameBody game, Role role, byte regType) {
        NameRegEntity nr = new NameRegEntity();
        nr.setRoleId(role.getUid());
        nr.setSrvId(game.config.srvId);
        nr.setRegTime(new Date());
        nr.setRegType(regType);
        nr.setGameId(role.getBase().gameId);
        nr.setGroupCode(game.config.gs.getGroupCode());
        return nr;
    }

    public static List<BatchMailEntity> queryBatchMail(GameBody game) {
        BatchMailListReq filter = new BatchMailListReq();
        filter.setSrvId(game.config.srvId);
        filter.all();
        BatchMailListResp resp = game.apis().console.batchMail.list(filter);
        return resp.getRecords();
    }

    public static int deleteBatchMail(GameBody game) {
        return game.apis().console.batchMail.deleteBySrvId(game.config.srvId);
    }
}
